package com.niguang.work.labour.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niguang.work.labour.api.entity.AdminBankInfo;
import com.niguang.work.labour.api.entity.Banks;
import com.niguang.work.labour.api.mapper.AdminBankInfoMapper;
import com.niguang.work.labour.api.service.AdminBankInfoService;
import com.niguang.work.labour.api.service.BanksService;
import com.niguang.work.labour.common.constant.ResultCode;
import com.niguang.work.labour.common.file.servie.FileService;
import com.niguang.work.labour.common.pojo.Result;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 83673
 * @since 2018-11-29
 */
@Service
public class AdminBankInfoServiceImpl extends ServiceImpl<AdminBankInfoMapper, AdminBankInfo> implements AdminBankInfoService {
    @Autowired
    BanksService userBankService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private FileService fileService;

    @Override
    public Result addBankInfo(AdminBankInfo adminBankInfo) {
        QueryWrapper<Banks> userBankQueryWrapper = new QueryWrapper<>();
        userBankQueryWrapper.eq(Banks.BANK_ID, adminBankInfo.getBankId()).last(" limit 1");
        Banks userBank = userBankService.getOne(userBankQueryWrapper);
        if (userBank == null) {
            return Result.build(ResultCode.FAILED, "请上传正确的银行卡!");
        }

        MultipartFile[] files = {adminBankInfo.getFile()};
        List<JSONObject> fileList = fileService.uploadFile(files);
        String picUrl = "";
        if (CollectionUtils.isNotEmpty(fileList)) {
            picUrl = fileList.get(0).getString("url");
        }
        AdminBankInfo insertInfo = new AdminBankInfo(picUrl, adminBankInfo.getBankId(), adminBankInfo.getBankName(), adminBankInfo.getBankAddress(), userBank.getBankName(), adminBankInfo.getLaborCompanyId(), adminBankInfo.getSiteId());
        Integer integer = baseMapper.insert(insertInfo);
        if (integer > 0) {
            return Result.build(ResultCode.SUCCESS, "添加专户信息成!");
        }
        return Result.build(ResultCode.FAILED, "添加专户信息失败!");
    }

    @Override
    public Result addBankPic(MultipartFile file, String key) {
        redisTemplate.delete(key);
        MultipartFile[] files = {file};
        List<JSONObject> fileList = fileService.uploadFile(files);
        if (CollectionUtils.isNotEmpty(fileList)) {
            redisTemplate.opsForValue().set(key, fileList.get(0).getString("url"), 5, TimeUnit.MINUTES);
            return Result.build(ResultCode.SUCCESS, "印鉴卡上传成功!");
        }
        return Result.build(ResultCode.FAILED, "印鉴卡上传失败!");
    }

    @Override
    public Result getBankPic(String key) {
        String picUrl = redisTemplate.opsForValue().get(key);
        if (picUrl != null) {
            return Result.success(picUrl);
        } else {
            return Result.success("");
        }
    }

    @Override
    public Result modifyBankInfo(AdminBankInfo adminBankInfo) {
        QueryWrapper<Banks> userBankQueryWrapper = new QueryWrapper<>();
        userBankQueryWrapper.eq(Banks.BANK_ID, adminBankInfo.getBankId()).last(" limit 1");
        Banks userBank = userBankService.getOne(userBankQueryWrapper);
        if (userBank == null) {
            return Result.build(ResultCode.FAILED, "请上传正确的银行卡!");
        }

        AdminBankInfo userInfo = baseMapper.selectById(adminBankInfo.getId());
        if (userInfo == null) {
            return Result.failed("请上传正确的id");
        }

        MultipartFile[] files = {adminBankInfo.getFile()};
        List<JSONObject> fileList = fileService.uploadFile(files);
        String picUrl = "";
        if (CollectionUtils.isNotEmpty(fileList)) {
            picUrl = fileList.get(0).getString("url");
        }


        adminBankInfo.setBankPic(picUrl);
        adminBankInfo.setBankName(userBank.getBankName());
        Integer integer = baseMapper.updateById(adminBankInfo);
        if (integer > 0) {
            return Result.build(ResultCode.SUCCESS, "更新专户信息成功!!");
        }
        return Result.failed("更新专户信息失败!");
    }

    @Override
    public Result getBankInfo(String id) {
        AdminBankInfo adminBankInfo = baseMapper.selectById(id);
        return Result.success(adminBankInfo);
    }
}
